<!-- 正文开始 -->
<div class="layui-fluid" id="careLabel">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar" lay-filter="careLabelSearch">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn btnSearch"><i class="layui-icon layui-icon-search"></i>搜索</button>
                        <button class="layui-btn icon-btn btnAdd"><i class="layui-icon layui-icon-add-1"></i>添加</button>
                        <button class="layui-btn icon-btn btnSave layui-btn-normal"><i class="layui-icon layui-icon-ok"></i>保存</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="careLabelTable" lay-filter="careLabelTable"></table>

        </div>
    </div>
    <!-- 表格操作列 -->
    <script type="text/html" class="tpl-careLabelTable-operator">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">修改</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <!-- 表格状态列 -->
    <script type="text/html" class="tpl-careLabelTable-status">
        <input type="checkbox" lay-filter="tpl-careLabelTable-status" value="{{d.id}}" lay-skin="switch" lay-text="有效|无效"
               {{d.status==1?'checked':''}}/>
    </script>

    <!-- 表格标签名称列 -->
    <script type="text/html" class="tpl-careLabelTable-name">
        <span style="background-color:{{d.bgColor}};color:{{d.fontColor}};" class="jzr-full-cell">{{d.labelName}}</span>
    </script>

    <!-- 表格sort列 -->
    <script type="text/html" class="tpl-careLabelTable-sort">
        <input type="text" value="{{d.sort}}" class="jzr-full-cell"/>
    </script>

</div>


<script>
    layui.use(['layer', 'table', 'form', 'http', 'tools'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var table = layui.table;
        var form = layui.form;
        var http = layui.http;
        var tools = layui.tools;

        var viewId = "#careLabel";
        var $view = $(viewId);

        // 渲染表格
        var careLabelTableIns = table.render({
            elem: '#careLabelTable',
            url: http.base_server + 'careLabel/list',
            method: 'GET',
            where: {},
            page: false,
            cellMinWidth: 50,
            hide: true,
            height: 'full-200',
            cols: [[
                {type: 'numbers'},
                {field: 'labelCode', sort: false, title: '标签编号',width:80, hide: true},
                {field: 'labelName', sort: false, title: '标签名称',width:200, templet: $view.find('.tpl-careLabelTable-name')},
                {field: 'sort', sort: true, title: '排序',width:50, align:"center",edit:'text'},
                {field: 'bgColor', sort: false, title: '背景颜色',width:100},
                {field: 'fontColor', sort: false, title: '字体颜色', width:100},
                {field: 'detailContent', sort: false, title: '详细内容',width:200},
                {field: 'status', sort: false, title: '状态', templet: $view.find('.tpl-careLabelTable-status'),width:80,align:"center"},
                {align: 'center', toolbar: $view.find('.tpl-careLabelTable-operator'), title: '操作', width: 100},
                {field: 'updateTime', sort: false, title: '更新时间'},
                {field: 'updateByWho', sort: false, title: '更新人'},
                {field: 'labelType', sort: false, title: '标签类型',width:80,hide:true},
                {field: 'remark', sort: false, title: '备注',width:80,hide:true}
            ]],
            error: function(code,msg){
                http.fail(code,msg);
            }
        });

        // 修改状态
        form.on('switch(tpl-careLabelTable-status)', function (obj) {
            var idx = obj.othis.parents("tr:eq(0)").attr("data-index");
            var data = {id: obj.elem.value, status: obj.elem.checked ? 1 : 0};
            careLabelTableIns.saveDelta(idx,data);
        });


        // 显示表单弹窗
        var showEditModel = function showEditModel() {
            tools.param.tableIns = careLabelTableIns;

            tools.open({
                title: tools.param.action === 2 ? '修改护理卡片' : '添加护理卡片',
                url: http.viewPath + '/patient/careLabelForm.html?v=1',
                area: ['600px', '480px'],
                success: function (layero, index) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');  // 禁止出现滚动条
                }
            });
        };

        table.on('tool(careLabelTable)', function (rowObj) {
            var data = rowObj.data;
            var layEvent = rowObj.event;

            if (layEvent === 'del') { // 删除
                layer.confirm("确定删除该行?",{btn:["确定","取消"]},
                    function(index,obj) {
                        layer.closeAll('dialog');
                        rowObj.del();
                    }
                );
            }
            if(layEvent === 'edit') { //编辑
                var param = tools.param = {};
                param.rowObj = rowObj;
                param.action = 2;
                param.data = data;
                showEditModel();
            }
        });

        //搜索按钮
        $view.find(".btnSearch").click(function () {
            careLabelTableIns.reload();
        });
        //增加按钮
        $view.find(".btnAdd").click(function(){
            var param = tools.param = {};
            param.action = 1;
            showEditModel();
        });

        table.on('edit(careLabelTable)',function (row) {
            var idx = row.tr.attr("data-index");
            var data = {};
            data.id = row.data.id;
            data[row.field] = row.value;
            careLabelTableIns.saveDelta(idx,data);
        });

        $view.find(".btnSave").click(function () {
            var data = careLabelTableIns.getDelta();
            if(!data || data.length <= 0){return;}
            http.post('careLabel/saveBatch',JSON.stringify(data),function(res){
                if(res.ok){
                    careLabelTableIns.updateCache(res.data);
                    careLabelTableIns.resetDelta();
                    http.okMsg(res.msg);
                }
            });
        });
    });
</script>